package com.example.controller;

import com.example.common.Result;
import com.example.entity.Travels;
import com.example.service.TravelsService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/travels")
public class TravelsController {

    @Resource
    private TravelsService travelsService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Travels travels) {
        travelsService.add(travels);
        return Result.success(travels); //todo 笔记：用户发布游记之后，页面应该变成编辑，所以将用户发布的游记再返回回去
    }

    /**
     * 更新（修改）
     */
    @PutMapping("/update")
    public Result update(@RequestBody Travels travels) {
        travelsService.updateById(travels);
        return Result.success();
    }

    /**
     * 更新阅读量
     */
    @PutMapping("/updateReadCount/{id}")
    public Result updateReadCount(@PathVariable Integer id) {
        travelsService.updateReadCount(id);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        travelsService.deleteByid(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/deleteBatch")
    public Result delete(@RequestBody List<Integer> ids) {
        travelsService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 根据id查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Travels travels = travelsService.selectById(id);
        return Result.success(travels);
    }

    /**
     * 查询所有（条件查询）
     */
    @GetMapping("/selectAll")
    public Result selectAll(Travels travels) {
        List<Travels> list = travelsService.selectAll(travels);
        return Result.success(list);
    }

    /**
     * 分页查询（在查询所有的基础上进行分页）
     */
    @GetMapping("/selectPage")
    public Result selectPage(Travels travels,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10")Integer pageSize) {
        PageInfo<Travels> pageInfo = travelsService.selectPage(travels,pageNum,pageSize);
        return Result.success(pageInfo);
    }

    /**
     * 分页查询（用户查询自己发布的游记）
     */
    @GetMapping("/selectPersonPage")
    public Result selectPersonPage(Travels travels,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10")Integer pageSize) {
        PageInfo<Travels> pageInfo = travelsService.selectPersonPage(travels,pageNum,pageSize);
        return Result.success(pageInfo);
    }
}
